﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using BrainTechLLC.Silverlight.InputControls;

namespace BrainTechLLC.Silverlight.InputControls2
{
    [TemplatePart(Name = "txtInput", Type = typeof(TextBox))]
    public partial class InputTextControl : InputBaseControl, IInputControl2
    {
        protected TextBox _txtInput;

        protected List<string> _additionalPrompts;
        protected List<string> _highlight = new List<string>();

        public static readonly DependencyProperty ColorForAdditionalBulletTextProperty = DependencyProperty.Register(
            "ColorForAdditionalBulletText", typeof(Brush), typeof(InputTextControl), new PropertyMetadata(InputControlConstants.FunkyColor, new PropertyChangedCallback(ColorForAdditionalBulletTextChanged)));

        private static void ColorForAdditionalBulletTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            InputTextControl t = (InputTextControl)d;
            t.ColorForAdditionalBulletText = e.NewValue as Brush;
            UpdateColorForAdditionalBulletTextBindings(t, e.NewValue as Brush);
        }

        public static void UpdateColorForAdditionalBulletTextBindings(InputTextControl obj, Brush newBackground)
        {
            obj.UpdateColorForAdditionalBulletTexts();
        }

        public Brush ColorForAdditionalBulletText
        {
            get { return (Brush)GetValue(ColorForAdditionalBulletTextProperty); }
            set { SetValue(ColorForAdditionalBulletTextProperty, value); }
        }

        public static readonly DependencyProperty AdditionalPromptsProperty = DependencyProperty.Register(
            "AdditionalPrompts", typeof(List<string>), typeof(InputTextControl), new PropertyMetadata(null, new PropertyChangedCallback(AdditionalPromptsChanged)));

        private static void AdditionalPromptsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            InputTextControl t = (InputTextControl)d;            
            UpdateAdditionalPromptsBindings(t, e.NewValue as List<string>);
        }

        public static void UpdateAdditionalPromptsBindings(InputTextControl obj, List<string> newPrompts)
        {
            obj.UpdateAdditionalPrompts(newPrompts);
        }

        public void UpdateAdditionalPrompts(List<string> newPrompts)
        {
            if (AdditionalPrompts != null)
            {
                RemoveAdditionalPrompts();
            }

            if (newPrompts != null)
            {
                AdditionalPrompts = newPrompts;

                foreach (string s in newPrompts)
                {
                    InsertAdditionalPromptText("● " + s);
                }

                _stackTop.Height += (_additionalPrompts.Count * 16);
                _stackTop.UpdateLayout();
                this.Height += (_additionalPrompts.Count * 16);
                this.UpdateLayout();
            }
        }

        public List<string> AdditionalPrompts
        {
            get { return (List<string>)GetValue(AdditionalPromptsProperty); }
            set { SetValue(AdditionalPromptsProperty, value); }
        }

        public void UpdateColorForAdditionalBulletTexts()
        {
            if (_stackTop != null)
            {
                for (int n = _stackTop.Children.Count - 5; n >= 1; n--)
                    (_stackTop.Children[n] as Control).Foreground = ColorForAdditionalBulletText;
            }
        }
        
        private void InsertAdditionalPromptText(string s)
        {
            TextBlock tb = new TextBlock()
            {
                // use 0 for bottom margin?
                Margin = new Thickness(32, 0, 0, 1),
                Text = s,
                HorizontalAlignment = HorizontalAlignment.Stretch
            };

            if (_highlight.Contains(s))
                tb.Foreground = InputControlConstants.FunkyColor;

            _stackTop.Children.Insert(_stackTop.Children.Count - 4, tb);
        }

        private void RemoveAdditionalPrompts()
        {
            for (int n = _stackTop.Children.Count - 5; n >= 1; n--)
                _stackTop.Children.RemoveAt(n);

            _stackTop.Height -= (_additionalPrompts.Count * 16);
            _stackTop.UpdateLayout();
            this.Height -= (_additionalPrompts.Count * 16);
            this.UpdateLayout();
        }

        public override void UpdateBackgroundForInputControlFocus()
        {
            if (_txtInput != null) _txtInput.Background = BackgroundForControlFocus;
        }

        /// <summary>
        /// Returns the primary child control responsible for receiving user input
        /// </summary>
        public override Control PrimaryInputControl { get { return _txtInput; } }

        public InputTextControl()
            : base()
        {
            DefaultStyleKey = typeof(InputTextControl);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _txtInput = base.GetTemplateChild("txtInput") as TextBox;

            _txtInput.GotFocus += new RoutedEventHandler(txtInput_GotFocus);
            _txtInput.KeyUp += new KeyEventHandler(txtInput_KeyUp);
            _txtInput.KeyDown += new KeyEventHandler(txtInput_KeyDown);
            _txtInput.LostFocus += new RoutedEventHandler(txtInput_LostFocus);

            _txtInput.FontSize = 8.5f;

            UpdateColorForAdditionalBulletTexts();
        }

        private void txtInput_LostFocus(object sender, RoutedEventArgs e)
        {
            OnValueChanged(Value);
            // Revalidate();
        }

        public override string Value
        {
            get
            {
                if (_txtInput != null)
                    return _txtInput.Text.Trim();

                return string.Empty;
            }
            set
            {
                if (Value != value && _txtInput != null)
                {
                    _txtInput.Text = value;
                    OnValueChanged(value);
                }
            }
        }

        private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            // HEREHERE
        }

        private void txtInput_KeyUp(object sender, KeyEventArgs e)
        {
            // HEREHERE
        }

        private void txtInput_GotFocus(object sender, RoutedEventArgs e)
        {
            //if (!ShiftKeyDown)
            //    _txtInput.Focus();

            //App.MainPage.mainReviewView.HandleAdjustScroll(this);
        }
    }
}


